package com.anlogic.sdk.bootimage.internal.ui.composite;

import com.anlogic.sdk.bootimage.internal.ui.AutoCompleteProvider;
import com.anlogic.sdk.bootimage.internal.ui.common.BootgenCheckBoxField;
import com.anlogic.sdk.bootimage.internal.ui.common.BootgenComboViewer;
import com.anlogic.sdk.bootimage.internal.ui.common.BootgenTextField;
import com.anlogic.sdk.bootimage.internal.ui.common.IFileBrowseInput;
import com.anlogic.sdk.bootimage.internal.ui.common.IFileBrowseInputInitListener;
import com.anlogic.sdk.bootimage.internal.ui.common.IModifyListener;
import com.anlogic.sdk.bootimage.model.BifReader;
import com.anlogic.sdk.bootimage.model.BootGenModel;
import com.anlogic.sdk.utils.FileUtils;
import com.anlogic.sdk.utils.StringUtils;
import java.io.File;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;

public class BasicSectionComposite extends AbsBootgenComposite {
	private static final String[] OUTPUT_TYPES = new String[] { "BIN", "MCS" };

	private IModifyListener<String> outBifPathLisener;

	private BootgenTextField outBifFileField;

	private BootgenComboViewer<String> formatTypeField;

	private BootgenTextField outImageField;

	private BootgenTextField fillBytesTextField;
	
	private BootgenCheckBoxField enableQspiX1Field;
	private BootgenCheckBoxField enableMmcClkField;

	private static final String DEFAULT_BIF = "output.bif";

	public BasicSectionComposite(Composite parent, BootGenModel model,
			AutoCompleteProvider.IAutoCompeletHelper autoCompleteHelper) {
		super(parent, 0, model, autoCompleteHelper);
		createUI(this);
	}

	private void createUI(Composite container) {
		setLayout((Layout) new GridLayout(3, false));
		this.outBifFileField = new BootgenTextField(this);
		this.outBifFileField.createSaveBrowseField(container, "Output BIF file path", "bootgen_files_selection",
				BIF_EXTENSIONS);
		this.outBifFileField.setDefaultGridData(1);
		Composite formatTypeComposite = new Composite(container, 0);
		GridDataFactory.swtDefaults().span(3, 1).applyTo((Control) formatTypeComposite);
		GridLayoutFactory.fillDefaults().numColumns(3).applyTo(formatTypeComposite);
		this.formatTypeField = new BootgenComboViewer<>(this);
		this.formatTypeField.createField(formatTypeComposite, "Output format:", (Object[]) OUTPUT_TYPES);
		GridDataFactory.swtDefaults().applyTo((Control) this.formatTypeField.getControl());
		this.outImageField = new BootgenTextField(this);
		this.outImageField.createSaveBrowseField(container, "Output path", "bootgen_files_selection",
				new String[] { "*.bin;*.mcs" });
		this.outImageField.setDefaultGridData(1);

		this.fillBytesTextField = new BootgenTextField(this);
		this.fillBytesTextField.createField(container, "Fill Bytes: ");
		GridDataFactory.swtDefaults().hint(50, SWT.DEFAULT).applyTo((Control) this.fillBytesTextField.getControl());
		
		Composite extraComposite = new Composite(container, 0);
		GridDataFactory.swtDefaults().span(2, 1).applyTo((Control) extraComposite);
		GridLayoutFactory.fillDefaults().numColumns(2).applyTo(extraComposite);
		this.enableQspiX1Field = new BootgenCheckBoxField(this);
		this.enableQspiX1Field.createField(extraComposite, "Use Qspi X1 Mode");
		GridDataFactory.swtDefaults().span(1, 1).applyTo((Control) this.enableQspiX1Field.getControl());
		this.enableMmcClkField = new BootgenCheckBoxField(this);
		this.enableMmcClkField.createField(extraComposite, "Use SD/EMMC 400KHz");
		GridDataFactory.swtDefaults().span(1, 1).applyTo((Control) this.enableMmcClkField.getControl());
	}

	public void loadUI() {
		this.outBifFileField.setSelection(this.model.getBifFile());
		this.formatTypeField.setSelection(this.model.getFormatType());
		String outputFile = this.model.getOutputFile();
		if (StringUtils.isNotEmpty(outputFile)) {
			this.outImageField.setSelection(outputFile);
		} else {
			updateOutputFile();
		}
		this.outImageField.setEnabled(!this.model.isSplit());
		this.fillBytesTextField.setSelection(this.model.getFillBytes());
		
		this.enableQspiX1Field.setSelection(Boolean.valueOf(this.model.isQspiX1()));
		this.enableMmcClkField.setSelection(Boolean.valueOf(this.model.isMmcClk()));
	}

	private void updateOutputFile() {
		refreshOutImageField();
		this.model.setOutputFile(this.outImageField.getSelection());
	}

	public void addListeners() {
		this.outBifFileField.setBrowseInputInitListener(new IFileBrowseInputInitListener() {
			public void initialize(IFileBrowseInput fileBrowseInput) {
				BasicSectionComposite.updateBifBrowseInput(BasicSectionComposite.this.outBifFileField.getSelection(),
						fileBrowseInput);
			}
		});
		this.outBifFileField.setModifyListener(new IModifyListener<String>() {
			public void valueChanged(String outBifPath) {
				BasicSectionComposite.this.outBifPathLisener.valueChanged(outBifPath);
				BasicSectionComposite.this.model.setBifFile(outBifPath);
				BasicSectionComposite.this.model.setOutputFile("");
				BasicSectionComposite.this.refreshOutImageField();
			}
		});
		this.formatTypeField.setModifyListener(new IModifyListener<String>() {
			public void valueChanged(String formatType) {
				BasicSectionComposite.this.model.setFormatType(formatType);
				BasicSectionComposite.this.refreshOutImageField();

			}
		});
		this.outImageField.setBrowseInputInitListener(new IFileBrowseInputInitListener() {
			public void initialize(IFileBrowseInput fileBrowseInput) {
				BasicSectionComposite.updateImageBrowseInput(BasicSectionComposite.this.outImageField.getSelection(),
						(String) BasicSectionComposite.this.formatTypeField.getSelection(), fileBrowseInput);
			}
		});
		this.outImageField.setModifyListener(new IModifyListener<String>() {
			public void valueChanged(String outImagePath) {
				BasicSectionComposite.this.model.setOutputFile(outImagePath);
				BasicSectionComposite.this.setSkipNotifactions(true);
				autoSelectFormatType(outImagePath);
				BasicSectionComposite.this.setSkipNotifactions(false);
				BasicSectionComposite.this.markEditedAndValidate();
			}

			public void autoSelectFormatType(String outImagePath) {
				String fileExtension = FileUtils.getFileExtension((new File(outImagePath)).getName());
				byte b;
				int i;
				String[] arrayOfString;
				for (i = (arrayOfString = BasicSectionComposite.OUTPUT_TYPES).length, b = 0; b < i;) {
					String formatType = arrayOfString[b];
					if (formatType.equalsIgnoreCase(fileExtension)) {
						BasicSectionComposite.this.formatTypeField.setSelection(formatType);
						BasicSectionComposite.this.model.setFormatType(formatType);
					}
					b++;
				}
			}
		});

		this.fillBytesTextField.setModifyListener(new IModifyListener<String>() {
			public void valueChanged(String fillBytes) {
				BasicSectionComposite.this.model.setFillBytes(fillBytes);
			}
		});
		
		this.enableQspiX1Field.setModifyListener(new IModifyListener<Boolean>() {
			public void valueChanged(Boolean newValue) {
				BasicSectionComposite.this.model.setQspiX1(newValue.booleanValue());
				BasicSectionComposite.this.markEditedAndValidate();
			}
		});
		this.enableMmcClkField.setModifyListener(new IModifyListener<Boolean>() {
			public void valueChanged(Boolean newValue) {
				BasicSectionComposite.this.model.setMmcClk(newValue.booleanValue());
				BasicSectionComposite.this.markEditedAndValidate();
			}
		});
	}

	public void setOutBifPathLisener(IModifyListener<String> outBifPathLisener) {
		this.outBifPathLisener = outBifPathLisener;
	}

	private void refreshOutImageField() {
		this.outImageField.setEnabled(!this.model.isSplit());
		String outImagePath = BifReader.computeOutImagePath(this.model);
		this.outImageField.setSelection(outImagePath);
	}

	public static void updateBifBrowseInput(String bifPath, IFileBrowseInput fileBrowseInput) {
		String filterPath = null;
		String fileName = null;
		if (StringUtils.isNotEmpty(bifPath)) {
			File bifFile = new File(bifPath);
			if (bifFile.isDirectory()) {
				filterPath = bifFile.getAbsolutePath();
				fileName = DEFAULT_BIF;
			} else {
				filterPath = bifFile.getParentFile().getAbsolutePath();
				fileName = bifFile.getName();
			}
		} else {
			fileName = DEFAULT_BIF;
		}
		fileBrowseInput.setFileName(fileName);
		fileBrowseInput.setFilterPath(filterPath);
	}

	public static void updateImageBrowseInput(String bootImagePath, String formatType,
			IFileBrowseInput fileBrowseInput) {
		String filterPath = null;
		String fileName = null;
		String defaultBootImageFileName = BifReader.getDefaultBootImageFileName(formatType);
		if (StringUtils.isNotEmpty(bootImagePath)) {
			File bootImageFile = new File(bootImagePath);
			if (bootImageFile.isDirectory()) {
				filterPath = bootImageFile.getAbsolutePath();
				fileName = defaultBootImageFileName;
			} else {
				filterPath = bootImageFile.getParentFile().getAbsolutePath();
				fileName = bootImageFile.getName();
			}
		} else {
			fileName = defaultBootImageFileName;
		}
		fileBrowseInput.setFileName(fileName);
		fileBrowseInput.setFilterPath(filterPath);
	}
}
